using Youz.Common.Helper;
using Youz.Common.DAL;
using Youz.Common.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Youz.Common.BLL
{
    /// <summary>
    /// Sys_Config BLL Class
    /// </summary>
    public partial class Sys_ConfigBLL : BaseBLL
    {
        Sys_ConfigDAL dal;
        private const string CacheKey = "Sys_Config";

        public Sys_ConfigBLL()
        {
            dal = new Sys_ConfigDAL();
        }

        /// <summary>
        /// 参数缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key, string configType, bool isModify, string defaultValue, string memo)
        {
            Dictionary<string, string> dic = CacheHelper.GetCache(CacheKey) as Dictionary<string, string>;
            if (dic == null)
            {
                dic = new Dictionary<string, string>();
                DataTable dt = dal.Gets("Status=1").Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    dic.Add(item["ConfigKey"].ToString(), item["ConfigValue"].ToString());
                }
                CacheHelper.SetCache(CacheKey, dic);
            }

            if (dic.ContainsKey(key))
            {
                return dic[key];
            }
            else if (!string.IsNullOrEmpty(defaultValue))
            {
                var cacheKey = string.Empty;
                Sys_Config model = new Sys_Config
                {
                    ConfigType = configType,
                    ConfigKey = key,
                    ConfigValue = defaultValue,
                    Memo = memo,
                    CreateTime = DateTime.Now,
                    Creator = "auto",
                    IsModify = isModify,
                    Status = 1
                };
                Add(model, out cacheKey);
                CacheHelper.RemoveCache(cacheKey);
                return defaultValue;
            }
            else
            {
                return dic[key];
            }
        }

        public bool SetValue(string key, string value)
        {
            Sys_Config model = GetOneModel(key);

            model.ConfigValue = value;
            model.LastModifier = "sys";
            model.LastModifyTime = ConfigParameters.SysTime;

            return dal.Update(model);
        }

        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Sys_Config model, out string key)
        {
            key = CacheKey;
            //if (dal.Exists(model.ConfigKey))
            //{
            //    return Utility.AjaxResult(false, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "RecordExists"));
            //}
            //CacheHelper.RemoveCache(CacheKey);
            Utility.WriteOperationLog("系統管理", "参数配置", string.Format("新增key:{0},value:{1}", model.ConfigKey, model.ConfigValue));
            return dal.Add(model);
        }
        /// <summary>
        /// 是否存在该KEY
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exists(string key)
        {
            if (dal.Exists(key))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Sys_Config model, out string key)//
        {
            key = CacheKey;
            //CacheHelper.RemoveCache(CacheKey);
            Utility.WriteOperationLog("系統管理", "参数配置", string.Format("更新key:{0},value:{1}", model.ConfigKey, model.ConfigValue));
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public string Delete(String ConfigKey, out string key)
        {
            key = CacheKey;
            //CacheHelper.RemoveCache(CacheKey);
            Utility.WriteOperationLog("系統管理", "参数配置", "刪除" + ConfigKey);
            return Utility.AjaxResult(dal.Delete(ConfigKey), AjaxResultType.Delete);
        }

        /// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetOneJson(string ConfigKey)
        {
            return Utility.GetJsonByObject(GetOneModel(ConfigKey));
        }

        /// <summary>
        /// 得到一个对象
        /// </summary>
        public Sys_Config GetOneModel(String ConfigKey)
        {
            return dal.GetOneModel(ConfigKey);
        }
        /// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetJson(string ConfigKey)
        {
            return Utility.GetJsonByObject(dal.GetModel(ConfigKey));
        }
        /// <summary>
        /// 得到一个对象
        /// </summary>
        public Sys_Config GetModel(String ConfigKey)
        {
            return dal.GetModel(ConfigKey);
        }
        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public string GetPager(string configKey, int pageindex, int pagesize)
        {
            int total = 0;
            DataTable dt = dal.GetPager(configKey, pageindex, pagesize, out total);
            return Utility.GetJsonByDataTableHaveTime(total, dt);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            return dal.Gets(sqlWhere);
        }
    }
}


